﻿Public Class frmCabinetMAJ
    Public pCode_cab As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmCabinetMAJ_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = "Fiche CABINET : " & pCode_cab

        CabinetToScreen()
        InitListeExperts(0)

    End Sub

    Private Sub CabinetToScreen()

        ' Nouvelle Création ?
        If pCode_cab = 0 Then
            ' RAB des champs du form
            ClearTextBoxes(Me)
            ChkAFFICHER.Checked = True
        Else
            'Lire le CABINET
            szSQL = "CALL spGET_CABINET('" & gSocieteCode & "'," & pCode_cab & ")"
            GetRecordset(ctDSN_DEVFAC, szSQL, rs)
            If rs.RecordCount < 1 Then
                rs.Close()
                Exit Sub
            End If

            TE_NOM.Text = rs.Fields("nom").Value
            TE_ADRESSE.Text = nv(rs.Fields("ADRESSE").Value)
            TE_ADRESSE2.Text = nv(rs.Fields("ADRESSE2").Value)
            TE_CP.Text = nv(rs.Fields("CP").Value)
            TE_VILLE.Text = nv(rs.Fields("ville").Value)
            TE_TEL1.Text = nv(rs.Fields("tel1").Value)
            TE_TEL2.Text = nv(rs.Fields("tel2").Value)
            TE_TELP.Text = nv(rs.Fields("TELP").Value)
            TE_FAX.Text = nv(rs.Fields("FAX").Value)
            ChkAFFICHER.Checked = rs.Fields("AFFICHER").Value
            rs.Close()

            InitListeExperts(0)
        End If

    End Sub

    Private Sub InitListeExperts(ByVal ix As Integer)
        Dim nb As Integer = 0

        ' effacer la listview
        LV_EXPERTS.Items.Clear()
        LV_EXPERTS.ListViewItemSorter = Nothing

        ' affiche les 50 CABINETS à partir de l'élève courant
        szSQL = "CALL spGET_EXPERTS('" & gSocieteCode & "'," & pCode_cab & ",0)"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_EXPERTS.Items.Add(rs.Fields("code_exp").Value)
            LV_EXPERTS.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_EXPERTS.Items(nb).SubItems.Add(nv(rs.Fields("tel1").Value))
            LV_EXPERTS.Items(nb).SubItems.Add(nv(rs.Fields("tel2").Value))
            LV_EXPERTS.Items(nb).SubItems.Add(nv(rs.Fields("email").Value))
            If rs.Fields("en_activite").Value = 0 Then
                LV_EXPERTS.Items(nb).ForeColor = Color.Gray
            End If
            nb = nb + 1
            ' Get the next record
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " fiche(s)"
        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            m_SortingColumn.Text = m_SortingColumn.Text.Replace("> ", "")
            SortListView(LV_EXPERTS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_EXPERTS)

    End Sub

    Private Sub BO_ENREGISTER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTER.Click

        If IsUserAuthorised(ctGestion) = False Then
            Exit Sub
        End If
        If TE_NOM.TextLength = 0 Then
            MsgBox("Nom du cabinet est obligatoire")
            Exit Sub
        End If

        szSQL = "CALL spUPD_P_CABINET('" & gSocieteCode & "'," & pCode_cab & ",'" & nq(TE_NOM.Text) & "','" & nq(TE_ADRESSE.Text) & "','" & _
                nq(TE_ADRESSE2.Text) & "','" & TE_CP.Text & "','" & nq(TE_VILLE.Text) & "','" & TE_TEL1.Text & "','" & _
                TE_TEL2.Text & "','" & TE_TELP.Text & "','" & TE_FAX.Text & "'," & CInt(ChkAFFICHER.Checked) & ");"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount > 0 Then
            pCode_cab = rs.Fields("newnumero").Value
        End If
        rs.Close()

        Me.Close()

    End Sub

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        If IsUserAuthorised(ctCompta) = False Then
            Exit Sub
        End If

        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        If pCode_cab = "1" Then
            MsgBox("Il est interdit de supprimer le cabinet Indéterminé !")
            Exit Sub
        End If

        deleteEnregistrement("P_CABINET", TE_NOM.Text, pCode_cab)
        ' ferme la fenetre
        Me.Close()

    End Sub

    Private Sub LV_EXPERTS_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_EXPERTS.DoubleClick

        If LV_EXPERTS.SelectedItems.Count > 0 Then
            ' afficher les détails de la ligne NoLigne
            frmCabinetEXP.pCode_cab = pCode_cab
            frmCabinetEXP.pCode_exp = LV_EXPERTS.SelectedItems(0).SubItems(0).Text
            frmCabinetEXP.ShowDialog()
            ' réaffiche la viewlist
            InitListeExperts(LV_EXPERTS.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_EXPERTS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_EXPERTS.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_EXPERTS_DoubleClick(sender, e)

            Case Keys.Insert        ' Insert = ' Ajouter un élève
                BO_ADD_LIGNE_Click(sender, e)
        End Select

    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click

        ' afficher les détails de la ligne NoLigne
        frmCabinetEXP.pCode_cab = pCode_cab
        frmCabinetEXP.pCode_exp = 0
        frmCabinetEXP.ShowDialog()
        ' réaffiche la viewlist
        InitListeExperts(0)

    End Sub

    Private Sub BO_IMPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_IMPRIMER.Click

        gCD_CLI = pCode_cab
        frmEditions.pRubrique = "CABINET"
        frmEditions.ShowDialog()

    End Sub
End Class